package com.dfish.downloadtoolNew;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

public class DDebug {
    public static boolean debug = true;
    public static final String logFileTag = "downloadtool";

    private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void logi(String msg) {
        if (!debug) {
            return;
        }
        Log.i(logFileTag, "" + msg);
        writeToFile(msg);
    }

    public static void logd(String msg) {
        if (!debug) {
            return;
        }
        Log.d(logFileTag, "" + msg);
        writeToFile(msg);
    }

    public static void loge(String msg) {
        if (!debug) {
            return;
        }
        Log.e(logFileTag, "" + msg);
        writeToFile(msg);
    }

    public static void logw(String msg) {
        if (!debug) {
            return;
        }
        Log.w(logFileTag, "" + msg);
        writeToFile(msg);
    }

    public static void writeToFile(String msg) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        try {
            String time = formatter.format(new Date());
            msg = time + ": " + msg + "\n";
            String file = "/mnt/sdcard/" + logFileTag + "log.txt";
            File f = new File(file);
            if (!f.exists()) {
                f.createNewFile();
            }
            Date d = new Date();
            String t = d.toLocaleString();
            msg = t + ":  " + msg + "\n";
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
            out.write(msg.toCharArray());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
